package com.example.f8399.testdemo;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.f8399.testdemo.activity.AnimationActivity;
import com.example.f8399.testdemo.activity.AvatorActivity;
import com.example.f8399.testdemo.activity.RecyclerMutiLayoutActivity;
import com.example.f8399.testdemo.activity.BaseQuicklyAdapterActivity;
import com.example.f8399.testdemo.activity.BottomNavigationActivity;
import com.example.f8399.testdemo.activity.BusActivity;
import com.example.f8399.testdemo.activity.ConstraintLayoutActivity;
import com.example.f8399.testdemo.activity.DialogActivity;
import com.example.f8399.testdemo.activity.DownloadActivity;
import com.example.f8399.testdemo.activity.EmptyRcActivity;
import com.example.f8399.testdemo.activity.ExpandableListViewActivity;
import com.example.f8399.testdemo.activity.FABActivity;
import com.example.f8399.testdemo.activity.FileExplorerActivity;
import com.example.f8399.testdemo.activity.FunctionActivity;
import com.example.f8399.testdemo.activity.LbsActivity;
import com.example.f8399.testdemo.activity.ManagerFragmentActivity;
import com.example.f8399.testdemo.activity.ObjectBoxActivity;
import com.example.f8399.testdemo.activity.OpenLightActivity;
import com.example.f8399.testdemo.activity.PagerLayoutManagerActivity;
import com.example.f8399.testdemo.activity.PermissionActivity;
import com.example.f8399.testdemo.activity.RecyclerViewActivity;
import com.example.f8399.testdemo.activity.RxAndroidManagerAc;
import com.example.f8399.testdemo.activity.RxBindingActivity;
import com.example.f8399.testdemo.activity.RxLaughingActivity;
import com.example.f8399.testdemo.activity.TakePhotoActivity;
import com.example.f8399.testdemo.activity.Test1Activity;
import com.example.f8399.testdemo.activity.TestBaseAdapterActivity;
import com.example.f8399.testdemo.activity.ViewSwitcherActivity;
import com.example.f8399.testdemo.adapter.RxAndroidAdapter;
import com.example.f8399.testdemo.bean.AppBean;
import com.example.f8399.testdemo.databinding.ActivityMainBinding;
import com.example.f8399.testdemo.entity.Test1Entity;
import com.example.f8399.testdemo.listener.OnItemClickListener;
import com.example.f8399.testdemo.utils.retrofit.AppApi;
import com.example.f8399.testdemo.utils.retrofit.ServiceGenerator;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends BaseActivity {

    ActivityMainBinding mBinding;
    private List<AppBean> mList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        initView();
    }

    public void goTo(View view) {
        AppApi.LoginApi loginApi = ServiceGenerator.createService(AppApi.LoginApi.class);
        Call<Test1Entity> login = loginApi.login();
        login.enqueue(new Callback<Test1Entity>() {
            @Override
            public void onResponse(Call<Test1Entity> call, Response<Test1Entity> response) {
                boolean successful = response.isSuccessful();
                ToastUtils.showShort("success:" + successful);
                LogUtils.d(response.body().getTitle());
            }

            @Override
            public void onFailure(Call<Test1Entity> call, Throwable t) {
                ToastUtils.showShort("failed");

            }

        });

    }

    private void initView() {
        mList.add(new AppBean("goto1", Test1Activity.class));
        mList.add(new AppBean("lbs", LbsActivity.class));
        mList.add(new AppBean("RxBinding的使用", RxBindingActivity.class));
        mList.add(new AppBean("permisssion", PermissionActivity.class));
        mList.add(new AppBean("ViewSwitcher", ViewSwitcherActivity.class));
        mList.add(new AppBean("Download", DownloadActivity.class));
        mList.add(new AppBean("Dialog", DialogActivity.class));
        mList.add(new AppBean("ObjcetBox", ObjectBoxActivity.class));
        mList.add(new AppBean("首页recyclerview多布局展示", RecyclerMutiLayoutActivity.class));
        mList.add(new AppBean("ExpandableListView", ExpandableListViewActivity.class));
        mList.add(new AppBean("RecyclerView", RecyclerViewActivity.class));
        mList.add(new AppBean("pagerLayoutManger", PagerLayoutManagerActivity.class));
        mList.add(new AppBean("Float Action Button  (FAB)", FABActivity.class));
        mList.add(new AppBean("EmptyRecyclerView", EmptyRcActivity.class));
        mList.add(new AppBean("开灯的可好玩了", OpenLightActivity.class));
        mList.add(new AppBean("练手布局的", ConstraintLayoutActivity.class));
        mList.add(new AppBean("BottomNavigation", BottomNavigationActivity.class));
        mList.add(new AppBean("take photo(拍照）", TakePhotoActivity.class));
        mList.add(new AppBean("file explorer 文件浏览", FileExplorerActivity.class));
        mList.add(new AppBean("测试各种功能函数", FunctionActivity.class));
        mList.add(new AppBean("打开Fragment管理器", ManagerFragmentActivity.class));
        mList.add(new AppBean("Rx Java", RxAndroidManagerAc.class));
        mList.add(new AppBean("树形 recyclerview", BaseQuicklyAdapterActivity.class));
        mList.add(new AppBean("retrfit+rxjava → 动态更改域名", RxLaughingActivity.class));
        mList.add(new AppBean("Bus        测试", BusActivity.class));
        mList.add(new AppBean("recyclerView 基类适配器测试", TestBaseAdapterActivity.class));
        mList.add(new AppBean("Viewpager", DownloadActivity2.class));
        mList.add(new AppBean("抖动动画效果", AnimationActivity.class));
        mList.add(new AppBean("点击头像", AvatorActivity.class));


        LinearLayoutManager layout = new LinearLayoutManager(this);
        mBinding.recyclerView.setLayoutManager(layout);
        mBinding.recyclerView.addItemDecoration(new DividerItemDecoration(this,layout.getOrientation()));
        RxAndroidAdapter adapter = new RxAndroidAdapter(this, mList);
        mBinding.recyclerView.setAdapter(adapter);
        //用于优化列表展示，不会出现半隐藏的item
        //参考：http://www.see-source.com/blog/300000037/10456.html
        LinearSnapHelper snapHelper = new LinearSnapHelper();
        snapHelper.attachToRecyclerView(mBinding.recyclerView);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                goActivity((Class<?>) mList.get(position).getCls());
            }
        });
    }
}
